package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25397
 * Date: 2022-03-27
 * Time: 11:03
 */
class Counter{
    //这个变量就是两个线程要去自增的变量
    public int count;

    synchronized public void increase(){
        count++;
    }
}
public class Demo15 {
    private static Counter counter=new Counter();

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });

        Thread t2=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });

        t1.start();

        t2.start();

        //在main中打印一下，两个线程自增完成后，得到的count值
        //这里要main要等待一下，否则t1，t2还没执行完main就打印了
        t1.join();
        t2.join();
        //ps:这里两个join谁在前，谁在后没有关系
        System.out.println(counter.count);
    }
}
